package com.jinlejia.erp.ui.house.prospect.dialog;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.LinearLayout;

import com.jinlejia.erp.R;
import com.jinlejia.erp.ui.house.prospect.bean.ReasonBean;
import com.wx.wheelview.adapter.BaseWheelAdapter;
import com.wx.wheelview.widget.WheelItem;
import com.wx.wheelview.widget.WheelView;

import java.util.List;

/**
 * @author 王进 on 2019/1/14.
 */

public class ProspectReasonDialog extends AlertDialog {
    private List<ReasonBean> mList;
    public ProspectReasonDialog(@NonNull Context context, List<ReasonBean> list) {
        super(context);
        this.mContext = context;
        this.mList = list;
    }

    private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        Window window = this.getWindow();
        // *** 主要就是在这里实现这种效果的.
        window.setContentView(R.layout.dialog_choose_reason);
        window.setGravity(Gravity.BOTTOM);
        window.setBackgroundDrawable(new ColorDrawable());

        window.setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        WheelView.WheelViewStyle style = new WheelView.WheelViewStyle();
        style.selectedTextColor = Color.parseColor("#F1BE0D");
        style.textColor = Color.GRAY;
        style.selectedTextSize = 20;
        style.holoBorderColor = Color.GRAY;

        final WheelView wv1 = window.findViewById(R.id.wheelview1);
        wv1.setWheelAdapter(new ArrayWheelAdapter(mContext));
        wv1.setWheelData(mList);
        wv1.setLoop(true);
        wv1.setSkin(WheelView.Skin.Holo);
        wv1.setStyle(style);
        wv1.setWheelSize(5);
        wv1.setExtraText("", Color.parseColor("#F1BE0D"), 38, 60);
        window.findViewById(R.id.confirm).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if( null != listener ){
                    ReasonBean bean = (ReasonBean) wv1.getSelectionItem();
                    listener.selectResult(bean);
                }
                dismiss();
            }
        });
        window.findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    private OnSelectListener listener;

    public void setListener(OnSelectListener listener) {
        this.listener = listener;
    }

    private static class ArrayWheelAdapter extends BaseWheelAdapter<String> {
        private Context mContext;

        ArrayWheelAdapter(Context context) {
            mContext = context;
        }

        @Override
        public View bindView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = new WheelItem(mContext);
            }
            WheelItem item = (WheelItem) convertView;
            item.setText(String.valueOf(mList.get(position)));
            return convertView;
        }
    }

    public interface OnSelectListener{
        void selectResult(ReasonBean result);
    }

}
